#-*- coding: utf-8 -*-


from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from utils import send_active_token
from trail import settings

class Account(models.Model):

    user = models.OneToOneField(User)
    nickname = models.CharField(u'昵称', max_length=40, blank=True, null=True)
    truename = models.CharField(u'实名', max_length=40, blank=True, null=True)
    phone = models.CharField(u'手机号', max_length=20, blank=True, null=True, unique=True)
    active_token = models.CharField(u'验证token', max_length=40, blank=True, null=True)
    is_valid = models.BooleanField(u'是否验证', default=False)
    last_login = models.DateTimeField(u'登录时间', auto_now=True)

    class Meta:
        verbose_name_plural = u'账号'

    def __unicode__(self):
        return '<Account phone:%s-eamil:%s>' % (self.phone, self.user.email)

    # def send_activation_email(self, site):
    #     ctx_dict = {'active_token': self.activation_key,
    #                 'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
    #                 'site': site}
    #     subject = render_to_string('email/active_email_subject.txt',
    #                                ctx_dict).encode('utf-8')
    #
    #     # Email subject *must not* contain newlines
    #     subject = ''.join(subject.splitlines())
    #
    #     message = render_to_string('email/active_email.txt',
    #                                ctx_dict).encode('utf-8')
    #
    #     self.user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
    #
    #
    #
    #
    # def activation_key_expired(self):
    #     expiration_date = datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS)
    #     return self.activation_key == u"ALREADY_ACTIVATED" or \
    #            (self.user.date_joined + expiration_date <= datetime.now())


#def send_active_email(sender, **kwargs):
#    accunt = kwargs['instance']
#    print kwargs
#    if kwargs['created'] and not accunt.is_valid():
#        email = accunt.user.email
#        send_active_token(host=settings.SITE_HOST, to_email=email, active_token=accunt.active_token)
#
#post_save.connect(send_active_email, sender=Account, dispatch_uid='send-active-email')

